﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.StockModule.IStockInOutViewModel
@{
    Layout = null;
}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.STOCKINOUT, FormMethod.Post, new { id = "formForStockIn" }))
{
    <div class="tool_bar">
        <input id="formForStockIn_save" type="submit" style="display: none;" />
        <input id="formForStockIn_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#formForStockIn_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-delete'">删除</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" onclick="javascript:$('#formForStockIn_reset').click()">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-print'">打印</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label>入库日期:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.InOutDate, new { id = "inOutDateForStockIn", style = "height:18px", @class = "Wdate", onfocus = "WdatePicker({minDate:'%y-%M-#{%d+1}'})" })
            </td>
            <td class="c100">
                <label>仓库:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.WareHouse.Id, new { id = "wareHouseCodeForStockIn" })
            </td>
            <td class="c100">
            </td>
            <td class="c160">
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>出入库类型:</label>
            </td>
            <td class="c160">
                @Html.DropDownListFor(m => m.Type, typeof(SystemIdentifier.DefaultStockInOutType), new { id = "typeForStockIn" })
            </td>
            <td class="c100">
                <label>入库单号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Id, new { id = "codeForStockIn" })
            </td>
            <td class="c100">
                <label>原始单据号:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.ProductionNumber, new { id = "productionNumberForStockIn" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>备注:</label>
            </td>
            <td colspan="4">
                @Html.TextBoxFor(m => m.Item.Remark, new { id = "remarkForStockIn" })
            </td>
            <td class="c160">
                <a>请选择常用备注</a>
            </td>
        </tr>
    </table>
    @Html.HiddenFor(m => m.StockInDetails, new { id = "stockInDetailsForStockIn" })
}
<table id="selectedStocksForStockIn" title="存货列表">
</table>
<input type="button" value="选择商品" id="selectStockForStockIn" />
<input type="button" value="确定商品" id="doSelectStockForStockIn" />
<table id="stocksForStockIn" title="原始存货列表"></table>
    <script type="text/javascript">
        $(function ()
        {
            var lastIndex;
            var salesDetails;
            $('#selectedStocksForStockIn').datagrid({
                columns: [[
                { field: 'Id', title: '存货编码', width: 100 },
                { field: 'Name', title: '存货名称', width: 80 },
                { field: 'Specification', title: '规格', width: 60 },
                { field: 'UnitTransfer', title: '计量单位', width: 100 },
                { field: 'Number', title: '数量', width: 100, editor: { type: 'text'} },
                { field: 'AssistantNumber', title: '辅助数量', width: 100 },
                { field: 'StandardPurchasePrice', title: '单价成本', width: 100, editor: { type: 'text'} },
                { field: 'Cost', title: '成本 ', width: 80, editor: { type: 'text'} },
                { field: 'Remark', title: '备注', width: 80 }
            ]]
            });

            $("#selectStockForStockIn").bind("click", function ()
            {
                $.ajax({
                    url: '/stock/list',
                    type: 'Post',
                    success: function (result)
                    {
                        if (result.total > 0)
                        {
                            $('#stocksForStockIn').datagrid({
                                columns: [[
                                { field: 'IsSelected', title: '选择', width: 60, checkbox: true },
                                { field: 'Type', title: '品类名称', width: 60 },
                                { field: 'Id', title: '存货编码', width: 100 },
                                { field: 'BarCode', title: '条形码', width: 80 },
                                { field: 'Name', title: '存货名称', width: 80 },
                                { field: 'Specification', title: '规格', width: 60 },
                                { field: 'UnitTransfer', title: '计量单位', width: 100 },
                                { field: 'LeftNumber', title: '现存数量', width: 100 },
                                { field: 'StandardPurchasePrice', title: '单价', width: 80 }
                            ]],
                                data: result,
                                checkOnSelect: true,
                                selectOnCheck: true
                            });
                        }
                    },
                    error: function ()
                    {
                        alert('系统繁忙，请稍候再试');
                    }
                })

                return false;
            });

            var currentIndex;
            var currentRow;
            var rowId;
            var standardPurchasePrice;
            var number;

            $("#doSelectStockForStockIn").bind("click", function ()
            {
                $('#selectedStocksForStockIn').datagrid('loadData', { total: 0, rows: [] });
                var rows = $('#stocksForStockIn').datagrid('getSelections');
                for (var i = 0; i < rows.length; i++)
                {
                    insert(rows[i].Id, rows[i].Name, rows[i].Specification, rows[i].UnitTransfer, 0, 0, rows[i].StandardPurchasePrice, 0, 'test');
                }
            });

            function insert(id, name, specification, unitTransfer, number, assistantNumber, standardPurchasePrice, cost, remark)
            {
                var rows = $('#selectedStocksForStockIn').datagrid('getRows');
                index = -1;
                if (rows.length == 0)
                {
                    index = 0;
                }
                else
                {
                    index = rows.length;
                }

                $('#selectedStocksForStockIn').datagrid('insertRow', {
                    index: index,
                    row: {
                        Id: id,
                        Name: name,
                        Specification: specification,
                        UnitTransfer: unitTransfer,
                        Number: number,
                        AssistantNumber: assistantNumber,
                        StandardPurchasePrice: standardPurchasePrice,
                        Cost: cost,
                        Remark: remark
                    }
                });
                $('#selectedStocksForStockIn').datagrid('selectRow', index);
                $('#selectedStocksForStockIn').datagrid('beginEdit', index);

                var ed = $('#selectedStocksForStockIn').datagrid('getEditors', index);
                for (var i = 0; i < ed.length; i++)
                {
                    var e = ed[i];
                    if (e.field == "StandardPurchasePrice")
                    {
                        $(e.target).attr("selectedStockRowIndex", index);
                        $(e.target).bind("click", function ()
                        {
                            $('#selectedStocksForStockIn').datagrid('selectRow', $(this).attr("selectedStockRowIndex"));
                        });
                        $(e.target).bind("blur", function ()
                        {
                            standardPurchasePrice = $(this).val();
                            currentIndex = $(this).attr("selectedStockRowIndex");
                            currentRow = $('#selectedStocksForStockIn').datagrid('getSelected');
                            rowId = $(this).parents().filter("tr").filter("[datagrid-row-index=" + currentIndex + "]").attr("id");
                            $("#" + rowId).find("td").each(function ()
                            {
                                if ($(this).attr("field") == "Number")
                                {
                                    number = $(this).find("input").val();
                                }
                            });
                            $("#" + rowId).find("td").each(function ()
                            {
                                if ($(this).attr("field") == "Cost")
                                {
                                    $(this).find("input").val(parseFloat(standardPurchasePrice) * parseFloat(number));
                                }
                            });
                            $('#selectedStocksForStockIn').datagrid('unselectRow', currentIndex);
                            currentRow.StandardPurchasePrice = standardPurchasePrice;
                        })
                    }
                    else if (e.field == "Number")
                    {
                        $(e.target).attr("selectedStockRowIndex", index);
                        $(e.target).bind("click", function ()
                        {
                            $('#selectedStocksForStockIn').datagrid('selectRow', $(this).attr("selectedStockRowIndex"));
                        });
                        $(e.target).bind("blur", function ()
                        {
                            number = $(this).val();
                            currentIndex = $(this).attr("selectedStockRowIndex");
                            currentRow = $('#selectedStocksForStockIn').datagrid('getSelected');
                            rowId = $(this).parents().filter("tr").filter("[datagrid-row-index=" + currentIndex + "]").attr("id");
                            $("#" + rowId).find("td").each(function ()
                            {
                                if ($(this).attr("field") == "StandardPurchasePrice")
                                {
                                    standardPurchasePrice = $(this).find("input").val();
                                }
                            });
                            $("#" + rowId).find("td").each(function ()
                            {
                                if ($(this).attr("field") == "Cost")
                                {
                                    $(this).find("input").val(parseFloat(standardPurchasePrice) * parseFloat(number));
                                }
                            });
                            $('#selectedStocksForStockIn').datagrid('unselectRow', currentIndex);
                            currentRow.Number = number;
                        })
                    }
                }
            }

            $("#formForStockIn").submit(function ()
            {
                var rows = $('#selectedStocksForStockIn').datagrid('getRows');
                stockInDetailsForStockIn = "[";
                for (var i = 0; i < rows.length; i++)
                {
                    if (i == (rows.length - 1))
                    {
                        stockInDetailsForStockIn = stockInDetailsForStockIn + "{ \"Stock\" : { \"Id\" : \"" + rows[i].Id + "\"}, " + "\"UnitCost\" :" + rows[i].StandardPurchasePrice + ", \"Number\" :" + rows[i].Number + " }";
                    }
                    else
                    {
                        stockInDetailsForStockIn = stockInDetailsForStockIn + "{ \"Stock\" : { \"Id\" : \"" + rows[i].Id + "\"}, " + "\"UnitCost\" :" + rows[i].StandardPurchasePrice + ", \"Number\" :" + rows[i].Number + " },";
                    }
                }
                stockInDetailsForStockIn = stockInDetailsForStockIn + "]";
                $("#stockInDetailsForStockIn").val(stockInDetailsForStockIn);
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: $(this).serialize(),
                    success: function (data)
                    {
                        if (data.IsSuccess)
                        {
                            alert(data.Messages[0]);
                        }
                    },
                    error: function ()
                    {
                        alert('系统繁忙，请稍候再试');
                    }
                })

                return false;
            });
        });
    </script>
